import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);

const actions = {
    addWang(context, data) {
        console.log('调用dispatch,进入actions');
        if (data.name.indexOf('王') === 0) {
            context.commit('ADDSTU', data);
        }else{
            alert('不姓王,添加失败')
        }
    },
    addSch(context, data) {
        if(data.name.trim() !== ''){
            context.commit('ADDSCH', data);
        }else{
            alert('学校名称为空')
        }
    },

    addHb(context, data) {
        if(data.name.indexOf('湖北') === 0){
            context.commit('ADDSCH', data);
        }else{
            alert('学校名称不是以湖北开头')
        }
    }
}

const mutations = {
    ADDSTU(state, data) {
        console.log('调用commit,进入mutations');
        state.students.push(data)
    },
    ADDSCH(state, data) {
        state.schools.push(data)

    }
}

const state = {
    students:[{name:'张三',id:'001'}],
    schoolName:'二师',
    schools:[{name:'光谷校区',id:'001'},{name:'武昌校区',id:'002'}],
}

const getters = {
    countWang(state){
        return state.students.filter((stu)=> stu.name.indexOf('王') === 0).length
    }
}

export default new Vuex.Store({
    actions,
    mutations,
    state,
    getters
})
